<?php
namespace Admin\Controller;

use Think\Controller;

class UploadController extends Controller
{

    public function index()
    {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 10240000;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            foreach ($info as $file) {
                echo json_encode($file['savepath'] . $file['savename']);
            }
        }
    }

    public function w_h($w = 750, $h = 500)
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 10240000;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录

        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            foreach ($info as $file) {

                $file_url = './Uploads/' . $file['savepath'] . $file['savename'];

                $image = new \Think\Image();
                $image->open($file_url);
                // 按照原图的比例生成一个最大为750*500的缩略图并保存为thumb.jpg
                $image->thumb($w, $h, \Think\Image::IMAGE_THUMB_NORTHWEST)->save($file_url);

                echo json_encode($file['savepath'] . $file['savename']);
            }
        }
    }

    public function image()
    {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 10240000;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        } else {// 上传成功
            foreach ($info as $file) {
                echo '/Uploads/' . $file['savepath'] . $file['savename'];
                exit;
            }
        }
    }

}